package com.lightkits.wms.controller;

import com.lightkits.framework.common.exception.LightKitsBizException;
import com.lightkits.framework.common.model.CheckResult;
import com.lightkits.framework.common.utility.object.ObjectUtil;
import com.lightkits.wms.domain.usuallyinterface.*;
import com.lightkits.wms.dto.usuallyinterface.*;
import com.lightkits.wms.dto.usuallyinterface.model.QuickCodeDto;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * UsuallyService
 *
 * @author jing.wei@hand-china.com
 * Created on 2019/4/18 14:52
 */
/*@LightKitsContract(serviceName = "UsuallyService", serviceNamespace = "center.soa.os-wms.com")*/
/*@SuppressWarnings("all")*/
@RestController
@RequestMapping("/UsuallyService")
public class UsuallyController {
    private final GetWmMoveTypeExecutor getWmMoveTypeExecutor;

    @Autowired
    public UsuallyController(GetWmMoveTypeExecutor getWmMoveTypeExecutor, GetWmAreaExecutor getWmAreaExecutor, GetWareAndAreaExecutor getWareAndAreaExecutor, GetSimTicketExecutor getSimTicketExecutor, GetSimMoveTypeExecutor getSimMoveTypeExecutor, GetBadMatReaExecutor getBadMatReaExecutor, GetCommBadMatReaExecutor getCommBadMatReaExecutor, GetQuickCodeService getQuickCodeService) {
        this.getWmMoveTypeExecutor = getWmMoveTypeExecutor;
        this.getWmAreaExecutor = getWmAreaExecutor;
        this.getWareAndAreaExecutor = getWareAndAreaExecutor;
        this.getSimTicketExecutor = getSimTicketExecutor;
        this.getSimMoveTypeExecutor = getSimMoveTypeExecutor;
        this.getBadMatReaExecutor = getBadMatReaExecutor;
        this.getCommBadMatReaExecutor = getCommBadMatReaExecutor;
        this.getQuickCodeService = getQuickCodeService;
    }
    private final GetQuickCodeService getQuickCodeService;

    @PostMapping(value = "/getWmMoveType")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getWmMoveType")
    @ResponseBody
    public GetWmMoveTypeResponseType getWmMoveType(@RequestBody(required = false) GetWmMoveTypeRequestType request) {
        return getWmMoveTypeExecutor.service(request);
    }


    @PostMapping(value = "/getWmArea")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getWmArea")
    @ResponseBody
    public GetWmAreaResponseType getWmArea(@RequestBody(required = false) GetWmAreaRequestType request) {
        return getWmAreaExecutor.service(request);
    }

    private final GetWmAreaExecutor getWmAreaExecutor;

    @PostMapping(value = "/getWareAndWm")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getWareAndWm")
    @ResponseBody
    public GetWareAndAreaResponseType getWareAndWm(@RequestBody(required = false) GetWareAndAreaRequestType request) {
        return getWareAndAreaExecutor.service(request);
    }

    private final GetWareAndAreaExecutor getWareAndAreaExecutor;

    @PostMapping(value = "/getSimTicket")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getSimTicket")
    @ResponseBody
    public GetSimTicketResponseType getSimTicket(@RequestBody(required = false)
                                                         GetSimTicketRequestType request) {
        return getSimTicketExecutor.service(request);
    }

    private final GetSimTicketExecutor getSimTicketExecutor;

    @PostMapping(value = "/getSimMoveType")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getSimMoveType")
    @ResponseBody
    public GetSimMoveTypeResponseType getSimMoveType(@RequestBody(required = false) GetSimMoveTypeRequestType request) {
        return getSimMoveTypeExecutor.service(request);
    }

    private final GetSimMoveTypeExecutor getSimMoveTypeExecutor;

    @PostMapping(value = "/getQuickValue")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getQuickValue")
    @ResponseBody
    public GetQuickValueResponseType getQuickValue(@RequestBody(required = false) GetQuickValueRequestType request) {
        GetQuickValueResponseType response = new GetQuickValueResponseType();
        response.setSuccessful(true);
        try {
            CheckResult<List<QuickCodeDto>> checkResult = getQuickCodeService.getQuickCode(request.getCode(),request.getValue(),request.getMeaning());
            if (!checkResult.isSuccessful() || checkResult.getData() == null) {
                return checkResult.fill(response);
            }
            response.setQuickCodeDtos(checkResult.getData());

        } catch (LightKitsBizException e) {
            response.setSuccessful(false);
            String mes= StringUtils.substringAfterLast(e.getMessage(),":");
            response.setMessage(ObjectUtil.isNullOrEmpty(mes)?e.getMessage():mes);
        } catch (Exception e) {
            e.printStackTrace();
            response.setSuccessful(false);
            response.setMessage("get Quick Code Faild");
        }
        return response;
    }
    @PostMapping(value = "/getBadMatReason")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getBadMatReason")
    @ResponseBody
    public GetBadMatReaResponseType getBadMatReason(@RequestBody(required = false) GetBadMatReaRequestType request) {
        return getBadMatReaExecutor.service(request);
    }

    private final GetBadMatReaExecutor getBadMatReaExecutor;
    @PostMapping(value = "/getCommBadMatReason")
    @Permission(permissionPublic = true)
    @ApiOperation(value = "/getCommBadMatReason")
    @ResponseBody
    public GetCommBadMatResponseType getCommBadMatReason(@RequestBody(required = false) GetCommBadMatRequestType request) {
        return getCommBadMatReaExecutor.service(request);
    }

    private final GetCommBadMatReaExecutor getCommBadMatReaExecutor;
}
